import { reqAddressInfo, reqOrderInfo, reqSubmitOrder } from "@/api"

const state = {
    addressInfo: [],
    orderInfo: {}
}
const mutations = {
    RECEIVE_ADDRESSINFO(state, data) {
        state.addressInfo = data
    },
    RECEIVE_ORDERINFO(state, data) {
        state.orderInfo = data
    }
}
const actions = {
    // 获取用户地址
    async getAddressInfo({ commit }) {
        const result = await reqAddressInfo()
        if (result.code === 200) {
            commit("RECEIVE_ADDRESSINFO", result.data)
        }
    },
    // 获取商品订单列表
    async getOderInfo({ commit }) {
        const result = await reqOrderInfo()
        if (result.code === 200) {
            commit("RECEIVE_ORDERINFO", result.data)
        }
    },
    // 提交订单
    async submitOrder({ commit }, { tradeNo, data }) {
        const result = await reqSubmitOrder(tradeNo, data)
        if (result.code == 200) {
            return 'ok'
        } else {
            return Promise.reject(new Error("faile"))
        }
    }
}
const getters = {
    totalAmount(state) {
        return state.orderInfo.totalAmount
    },
    totalNum(state) {
        return state.orderInfo.totalNum
    }
}
export default {
    state,
    mutations,
    actions,
    getters
}